var initConfig;
var initjson;
var stageView;
var moduleAnimation;
var webSocketClilent;
var stage_id;
var jyztEchartOption;
var widthParent;
var heightParent;
var messageContentTemp="";
$(function(){
    "use strict";
     moduleAnimation = new ModuleAnimation();
    layui.use(['layer'], function() {




        jyztEchartOption = new EchartOption();//echart组件配置
        //初始化场景
        var stagejson = window.opener.getStageStr();//父页面场景json
        stageView = new StageView();
        stageView.initStage(stagejson);

        var resizeTimer = null;

        if (isFullscreen()) { // 监听 狂口变化，切换全屏图标
            $("#full").attr("class","layui-icon layui-icon-screen-restore");
        }else{
            $("#full").attr("class","layui-icon layui-icon-screen-full");
        }
        //检测浏览器窗口改变 妖初始化场景，解决echart图表位置不变的问题
        window.onresize = function() {
            //setTimeout('stageView.adaptPhoneWindows()',"1000");
            if (!isFullscreen()) { // 监听 狂口变化，切换全屏图标
              $("#full").attr("class","layui-icon layui-icon-screen-full");
            }
            stageView.adaptPhoneWindows();

        }
        // 手机端浏览器自动旋转事件
        window.addEventListener('orientationchange', function () {
            document.location.reload();
        });
        //给页面绑定滑轮滚动事件
       // if (document.addEventListener) {//firefox
          //  document.addEventListener('DOMMouseScroll', scrollFunc, false);
        //}
        //滚动滑轮触发scrollFunc方法 //ie 谷歌
      //  window.onmousewheel = document.onmousewheel = scrollFunc;

        //全屏/还原
        $("#full").click( function () {
            if(isFullscreen()){// 下面的代码看起来可能有点怪，为了解决全屏状态下 切换画面后，不能还原。
                fullScreen();
                setTimeout(function(){
                    fullExit();
                },100);
            }else{
                fullScreen();
            }

        });




    });



  /*  $(document).mousemove(function(e) {
        if (!!this.move) {
            var posix = !document.move_target ? {'x': 0, 'y': 0} : document.move_target.posix,
                callback = document.call_down || function() {
                    $(this.move_target).css({
                        'top': e.pageY - posix.y-(100.5*0.2552083333333333/2),
                        'left': e.pageX - posix.x-(448-448*0.2552083333333333)
                    });
                };

            callback.call(this, e, posix);
        }
    }).mouseup(function(e) {
        if (!!this.move) {
            var callback = document.call_up || function(){};
            callback.call(this, e);
            $.extend(this, {
                'move': false,
                'move_target': null,
                'call_down': false,
                'call_up': false
            });
        }
    });

    $('#container').mousedown(function(e) {
        var offset = $(this).offset();
        this.posix = {'x': e.pageX - offset.left, 'y': e.pageY - offset.top};
        $.extend(document, {'move': true, 'move_target': this});
    });*/


});

function fullScreen() {
    $("#full").attr("class","layui-icon layui-icon-screen-restore");
    var element = document.documentElement; //若要全屏页面中div，var element= document.getElementById("divID");
    //IE 10及以下ActiveXObject
    if (window.ActiveXObject) {
        var WsShell = new ActiveXObject('WScript.Shell')
        WsShell.SendKeys('{F11}');
    }
    //HTML W3C 提议
    else if (element.requestFullScreen) {
        element.requestFullScreen();
    }
    //IE11
    else if (element.msRequestFullscreen) {
        element.msRequestFullscreen();
    }
    // Webkit (works in Safari5.1 and Chrome 15)
    else if (element.webkitRequestFullScreen) {
        element.webkitRequestFullScreen();
    }
    // Firefox (works in nightly)
    else if (element.mozRequestFullScreen) {
        element.mozRequestFullScreen();
    }
}

//退出全屏
function fullExit() {
    $("#full").attr("class","layui-icon layui-icon-screen-full");
    var element = document.documentElement; //若要全屏页面中div，var element= document.getElementById("divID");
    //IE ActiveXObject
    if (window.ActiveXObject) {
        var WsShell = new ActiveXObject('WScript.Shell')
        WsShell.SendKeys('{F11}');
    }
    //HTML5 W3C 提议
    else if (element.requestFullScreen) {
        document.exitFullscreen();
    }
    //IE 11
    else if (element.msRequestFullscreen) {
        document.msExitFullscreen();
    }
    // Webkit (works in Safari5.1 and Chrome 15)
    else if (element.webkitRequestFullScreen) {
        document.webkitCancelFullScreen();
    }
    // Firefox (works in nightly)
    else if (element.mozRequestFullScreen) {
        document.mozCancelFullScreen();
    }
}
// 判断是否全屏
function isFullscreen () {
    var explorer = window.navigator.userAgent.toLowerCase();
    if(explorer.indexOf('chrome')>0){//webkit
        if (document.body.scrollHeight === window.screen.height && document.body.scrollWidth === window.screen.width) {
            return true;
        } else {
            return false;
        }
    }else{//IE 9+  fireFox
        if (window.outerHeight === window.screen.height && window.outerWidth === window.screen.width) {
            return true;
        } else {
            return false;
        }
    }

}


// -------------------------------------------------防止右键控制台--------

//window.oncontextmenu = function () { return false;}
// window.onkeydown = window.onkeyup = window.onkeypress = function (event) {
//     var e = event || window.event || arguments.callee.caller.arguments[0];
//     if (e && e.keyCode == 123) {
//         layer.msg('控制台已禁止', {
//             offset: 't',
//             anim: 6,
//             icon:2
//         });
//         e.returnValue = false;
//         return (false);
//     }

// }
var ConsoleManager = {
    openCallback: function(){
        console.log('控制台打开了');
        try {
            window.open("about:blank", "_self")
        } catch(e) {
            var btn = document.createElement("button");
            btn.onclick = function() {
                window.open("about:blank", "_self")
            }
            btn.click()
        }
    },
    observer: function(){
        var obj = Object.create(null), t = Date.now(), that = this;
        Object.defineProperty(obj, "a", {
            get: function() {
                if(Date.now() - t > 100){
                    that.openCallback()
                }
            }
        });
        setInterval(function(){
            t = Date.now();
            (function(){})["constructor"]("debugger")();//debugger;
            console.log(obj.a);
        }, 200);
    },
    observerF: function(){
        var obj = Object.create(null), t = Date.now(), that = this;
        Object.defineProperty(obj, "a", {
            get: function() {
                if(Date.now() - t > 100){
                    that.openCallback()
                }
            }
        });
        setInterval(function(){
            t = Date.now();
            (function(){})["constructor"]("debugger")();//debugger;
            console.log(obj.a);
        }, 200);
    },
    init: function(){
        var t = window.navigator.userAgent.toLowerCase();
        t.indexOf("firefox") >= 0 ? this.observerF() : this.observer();
    }
}
//发送测试数据
function sendtestData(){
    stageView.setStageData(eval("("+$("#stageDatajson").val()+")"));
}